import pygame
import sys

# 模拟调用大模型 API 的函数
def call_large_model_api():
    # 假设这是从 API 获取的对话内容
    return "Hello world!"

# 初始化 Pygame
pygame.init()

# 设置窗口大小和标题
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pygame 大模型对话显示")

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)

# 设置字体
font = pygame.font.Font(None, 36)  # 使用默认字体，大小为 36

# 对话框区域
dialog_box_rect = pygame.Rect(50, 400, 700, 150)

# 主循环
def main():
    clock = pygame.time.Clock()
    running = True

    # 调用 API 获取对话内容
    dialog_text = call_large_model_api()

    while running:
        screen.fill(WHITE)  # 清屏

        # 绘制对话框
        pygame.draw.rect(screen, GRAY, dialog_box_rect)
        pygame.draw.rect(screen, BLACK, dialog_box_rect, 2)  # 边框

        # 将文本渲染到对话框中
        words = dialog_text.split(' ')
        lines = []
        current_line = ""
        for word in words:
            test_line = current_line + word + ' '
            test_surface = font.render(test_line, True, BLACK)
            if test_surface.get_width() > dialog_box_rect.width - 20:  # 自动换行
                lines.append(current_line)
                current_line = word + ' '
            else:
                current_line = test_line
        lines.append(current_line)  # 添加最后一行

        # 绘制每一行文本
        y_offset = dialog_box_rect.y + 10
        for line in lines:
            text_surface = font.render(line, True, BLACK)
            screen.blit(text_surface, (dialog_box_rect.x + 10, y_offset))
            y_offset += font.get_height()

        # 处理事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        pygame.display.flip()  # 更新屏幕
        clock.tick(30)  # 控制帧率

    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()